var url="https://item.jd.com/5523485.html#crumb-wrap";
var page = require('webpage').create();
//page.captureContent = [ /.*/ ];
page.settings.loadImages = false;
page.settings.javascriptEnabled=false;

var skuId = url.replace(/[^0-9]/ig,"");

page.onError = function(msg,trace){
	var msgStack = ['ERROR: ' + msg];
	if (trace && trace.length) {
		msgStack.push('TRACE:');
		trace.forEach(function(t) {
			msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
		});
	}
	// uncomment to log into the console 
	//console.error(msgStack.join('\n'));
	//phantom.exit();
};
page.open(url,function(status){
	var analyzeObj = page.evaluate(function(){
		//页面中dom提取的信息=========================================================
		var pageTitle = document.querySelector(".sku-name").textContent.trim();	//页面中标题
		var default_img = document.querySelector("#spec-img").getAttribute("data-origin");	//页面中默认图片地址
		
		var obj = {pageTitle:pageTitle,defaultImg:default_img}
		return obj;

	});
	
	var priceUrl = "https://p.3.cn/prices/mgets?skuIds=J_"+skuId;
	priceRequest(page,priceUrl,function(resBody){
		var retObj = {};
		retObj.platform = 'jd';
		retObj.analyzeObj = analyzeObj;
		retObj.priceObj = resBody;
		console.log(JSON.stringify(retObj));
		//page.close();
		phantom.exit();
	});
	
});
//获取价格请求
function priceRequest(page,priceurl,callBack){
	page.open(priceurl,function(status){
		callBack(JSON.parse(page.plainText));
	});
}